<?php

namespace SpecialOperations\GameBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * SpecialOperations\GameBundle\Entity\Building
 */
class Building
{
    const CoalMine = 'CoalMine';
    const Rafinery = 'Rafinery';
    const Metalworks = 'Metalworks';
    const PowerPlant = 'PowerPlant';
    const Airport = 'Airport';
    const Laboratory = 'Laboratory';
    const Garnison = 'Garnison';
    const DefenceStructures = 'DefenceStructures';
    const Headquaters = 'Headquaters';
    const Academy = 'Academy';
    const Market = 'Market';
    const CoalStorage = 'CoalStorage';
    const OilTank = 'OilTank';
    const MetalStorage = 'MetalStorage';
    
    /**
     * @var string $type
     */
    private $type;

    /**
     * @var bigint $level
     */
    protected $level = 0;


    /**
     * @var datetime $created
     */
    protected $created;

    /**
     * @var datetime $updated
     */
    protected $updated;

    /**
     * @var bigint $id
     */
    protected $id;

    /**
     * @var SpecialOperations\GameBundle\Entity\District
     */
    protected $districtid;
    

    /**
     * Set type
     *
     * @param string $type
     */
    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set level
     *
     * @param bigint $level
     */
    public function setLevel($level)
    {
        $this->level = $level;
    }

    /**
     * Get level
     *
     * @return bigint 
     */
    public function getLevel()
    {
        return $this->level;
    }

    /**
     * Set created
     *
     * @param datetime $created
     */
    public function setCreated($created)
    {
        $this->created = $created;
    }

    /**
     * Get created
     *
     * @return datetime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param datetime $updated
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;
    }

    /**
     * Get updated
     *
     * @return datetime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Get id
     *
     * @return bigint 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set districtid
     *
     * @param SpecialOperations\GameBundle\Entity\District $districtid
     */
    public function setDistrictid(\SpecialOperations\GameBundle\Entity\District $districtid)
    {
        $this->districtid = $districtid;
    }

    /**
     * Get districtid
     *
     * @return SpecialOperations\GameBundle\Entity\District 
     */
    public function getDistrictid()
    {
        return $this->districtid;
    }
    /**
     * @var float $condition
     */
    private $condition = 1;

    /**
     * @var SpecialOperations\GameBundle\Entity\BuildingQueue
     */
    private $queue;

    /**
     * @var SpecialOperations\GameBundle\Entity\District
     */
    private $district;


    /**
     * Set condition
     *
     * @param float $condition
     */
    public function setCondition($condition)
    {
        $this->condition = $condition;
    }

    /**
     * Get condition
     *
     * @return float 
     */
    public function getCondition()
    {
        return $this->condition;
    }

    /**
     * Set queue
     *
     * @param SpecialOperations\GameBundle\Entity\BuildingQueue $queue
     */
    public function setQueue(\SpecialOperations\GameBundle\Entity\BuildingQueue $queue)
    {
        $this->queue = $queue;
    }

    /**
     * Get queue
     *
     * @return SpecialOperations\GameBundle\Entity\BuildingQueue 
     */
    public function getQueue()
    {
        return $this->queue;
    }

    /**
     * Set district
     *
     * @param SpecialOperations\GameBundle\Entity\District $district
     */
    public function setDistrict(\SpecialOperations\GameBundle\Entity\District $district)
    {
        $this->district = $district;
    }

    /**
     * Get district
     *
     * @return SpecialOperations\GameBundle\Entity\District 
     */
    public function getDistrict()
    {
        return $this->district;
    }
    /**
     * @var integer $populationConsumption
     */
    private $populationConsumption = 0;


    /**
     * Set populationConsumption
     *
     * @param integer $populationConsumption
     */
    public function setPopulationConsumption($populationConsumption)
    {
        $this->populationConsumption = $populationConsumption;
    }

    /**
     * Get populationConsumption
     *
     * @return integer 
     */
    public function getPopulationConsumption()
    {
        return $this->populationConsumption;
    }
    public function __construct()
    {
        $this->queue = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Add queue
     *
     * @param SpecialOperations\GameBundle\Entity\BuildingQueue $queue
     */
    public function addBuildingQueue(\SpecialOperations\GameBundle\Entity\BuildingQueue $queue)
    {
        $this->queue[] = $queue;
    }

    /**
     * Add queue
     *
     * @param SpecialOperations\GameBundle\Entity\QueuePosition $queue
     */
    public function addQueuePosition(\SpecialOperations\GameBundle\Entity\QueuePosition $queue)
    {
        $this->queue[] = $queue;
    }
    
    public function getEnergyConsumption(){
        return 10*$this->getLevel()*pow(1.1, $this->getLevel());
    }
    
    public function getWorstMultiplier(){
        $distMultiplier=$this->getDistrict()->getWorstMultiplier();
        if($this->getCondition()>$distMultiplier){
            return $distMultiplier;
        }
        else{
            return $this->getCondition();
        }
    }
    
    /**
     * Returns cost of metal to upgrade building to next lvl
     * @return int 
     */
    public function getMetalCost(){
        return 30*($this->getLevel()+1)*pow(1.1, $this->getLevel());
    }
    
    /**
     * Returns cost of coal to upgrade building to next lvl
     * @return int 
     */
    public function getCoalCost(){
        return 20*($this->getLevel()+1)*pow(1.1, $this->getLevel());
    }
    
    /**
     * Returns cost of oil to upgrade building to next lvl
     * @return int 
     */
    public function getOilCost(){
        return 10*($this->getLevel()+1)*pow(1.1, $this->getLevel());
    }
    
    /**
     * Returns time needed to build next lvl of building (in seconds)
     * @return int 
     */
    public function getBuildingTime(){
        return ( $this->getMetalCost() + $this->getCoalCost() + $this->getOilCost() );
    }
    
    /**
     * returns time needed to colect resources to build next lvl of building
     * 0 means constructing can stars inmediatly 
     */
    public function getTimeToBuild(){
        $time=0;
        if($this->getDistrict()->getMetal()<$this->getMetalCost()){
            $time=$this->getDistrict()->getTimeToCollectMetal($this->getMetalCost());
        }
        if($this->getDistrict()->getCoal()<$this->getCoalCost()){
            $tmp=$time=$this->getDistrict()->getTimeToCollectCoal($this->getCoalCost());
            if($tmp>$time){
                $time=$tmp;
            }
        }
        if($this->getDistrict()->getOil()<$this->getOilCost()){
            $tmp=$this->getDistrict()->getTimeToCollectOil($this->getOilCost());
            if($tmp>$time){
                $time=$tmp;
            }
        }
    }
}